package  
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.ui.MouseCursorData;
	/**
	 * ...
	 * @author hbb
	 */
	public class CursorManager 
	{
		public static const DICE	:String = 'dice';
		public static const ARROW	:String = MouseCursor.ARROW;
		public static const AUTO	:String = MouseCursor.AUTO;
		public static const BUTTON	:String = MouseCursor.BUTTON;
		public static const HAND	:String = MouseCursor.HAND;
		public static const IBEAM	:String = MouseCursor.IBEAM;
		
		[Embed(source = '../lib/0000.jpg')] private const Dice0:Class;
		[Embed(source = '../lib/0001.jpg')] private const Dice1:Class;
		[Embed(source = '../lib/0002.jpg')] private const Dice2:Class;
		[Embed(source = '../lib/0003.jpg')] private const Dice3:Class;
		[Embed(source = '../lib/0004.jpg')] private const Dice4:Class;
		[Embed(source = '../lib/0005.jpg')] private const Dice5:Class;
		[Embed(source = '../lib/0006.jpg')] private const Dice6:Class;
		[Embed(source = '../lib/0007.jpg')] private const Dice7:Class;
		[Embed(source = '../lib/0008.jpg')] private const Dice8:Class;
		[Embed(source = '../lib/0009.jpg')] private const Dice9:Class;
		
		public static const instance:CursorManager = new CursorManager(CursorManagerLock);
		
		public function CursorManager(lock:Class) 
		{
			if (lock === CursorManagerLock)
			{
				init();
			}
			else
			{
				throw new ArgumentError('CursorManager is a Singleton');
			}
		}
		
		private function init():void 
		{
			var vb:Vector.<BitmapData> = new Vector.<BitmapData>();
			vb[0] = fixBitmapData(new Dice0().bitmapData);
			vb[1] = fixBitmapData(new Dice1().bitmapData);
			vb[2] = fixBitmapData(new Dice2().bitmapData);
			vb[3] = fixBitmapData(new Dice3().bitmapData);
			vb[4] = fixBitmapData(new Dice4().bitmapData);
			vb[5] = fixBitmapData(new Dice5().bitmapData);
			vb[6] = fixBitmapData(new Dice6().bitmapData);
			vb[7] = fixBitmapData(new Dice7().bitmapData);
			vb[8] = fixBitmapData(new Dice8().bitmapData);
			vb[9] = fixBitmapData(new Dice9().bitmapData);
			vb.fixed = true;
			
			var md:MouseCursorData = new MouseCursorData();
			md.data = vb;
			md.frameRate = 12;
			
			Mouse.registerCursor( DICE, md );
		}
		
		private function fixBitmapData(bd:BitmapData):BitmapData
		{
			var out:BitmapData
			
			const limit:int = 32;
			if (bd.width > limit || bd.height > limit)
			{
				var mat:Matrix = new Matrix();
				if (bd.width > bd.height) mat.scale(limit / bd.width, limit / bd.width);
				else mat.scale(limit / bd.height, limit / bd.height);
				out = new BitmapData(32, 32, bd.transparent, 0);
				out.draw(bd, mat);
			}
			else out = bd;
			
			return out;
		}
		
	}

}

class CursorManagerLock{}